module.exports = (app) => {
  const baseController = require("./base")(app);
  /**
   * 业务控制器
   * 继承自基础控制器
   */
  return class BusinessController extends baseController {
    getList(ctx) {
      const { product_name: productName = "" } = ctx.query;
      let productList = this.getProductList(ctx);
      if (productName && productName !== "all") {
        productList = productList.filter(
          (item) => item.product_name === productName
        );
      }
      this.success(ctx, productList);
    }
    remove(ctx) {
      this.success(ctx, {
        product_id: ctx.request.body.product_id,
      });
    }
    getProductEnumList(ctx) {
      this.success(ctx, [
        {
          label: "全部",
          value: "all",
        },
        {
          label: "名称1",
          value: 1,
        },
        {
          label: "名称2",
          value: 2,
        },
        {
          label: "名称3",
          value: 3,
        },
      ]);
    }
    create(ctx) {
      const { product_name: productName, price, inventory } = ctx.request.body;
      this.success(ctx, {
        product_id: "4",
        product_name: productName,
        price,
        inventory,
        create_time: new Date().toISOString(),
      });
    }
    update(ctx) {
      const { product_id: productId, product_name: productName, price, inventory } = ctx.request.body;
      this.success(ctx, {
        product_id: productId,
        product_name: productName,
        price,
        inventory,
        create_time: new Date().toISOString(),
      });
    }
    get(ctx) {
      const { product_id: productId } = ctx.request.query;
      const productList = this.getProductList(ctx);
      const product = productList.find((item) => item.product_id === productId);
      if (!product) {
        this.fail(ctx, "产品不存在");
        return;
      }
      this.success(ctx, product);
    }
    getProductList(ctx) {
      return [
        {
          product_id: "1",
          product_name: `《大前端面试宝典》${ctx.request.projKey}`,
          price: 39.9,
          inventory: 99999,
          create_time: "2023-07-03 20:23:22",
        },
        {
          product_id: "2",
          product_name: "《前端求职之道》",
          price: 199,
          inventory: 1000000,
          create_time: "2023-02-14 22:12:44",
        },
        {
          product_id: "3",
          product_name: "《大前端全栈实践》",
          price: 899,
          inventory: 18888,
          create_time: "2024-11-17 11:02:01",
        },
      ]
    }
  };
};
